package org.tema.web;

import javax.ejb.EJB;
import org.tema.ejb.UserBeanLocal;
import org.tema.entities.User;

import java.util.Arrays;
import java.util.List;

public class RegisterBean {
	
	@EJB
	private UserBeanLocal userBean;
	
	private String name;
	private String password;
	private Boolean isAdmin;
	private User user;
	private String newName;
	private String newPassword;
	private Boolean newPrivilige;
	private List <User> userList;
	
	
	public RegisterBean(){
		
	}
	
	public String getName() {
		/*
		user =  userBean.findUser(this.name);
		if (user != null){
			return user.getName();
		}
		else{
			return "user not found";
		}
		*/
		return this.name;
	}
	
	
	
	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
	
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean getIsAdmin() {

		return this.isAdmin;
	}

	public void setIsAdmin(Boolean isAdmin) {
		this.isAdmin = isAdmin;
	}


	public String getNewName() {
		return newName;
	}



	public void setNewName(String newName) {
		this.newName = newName;
	}



	public String getNewPassword() {
		return newPassword;
	}



	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}



	public Boolean getNewPrivilige() {
		return newPrivilige;
	}



	public void setNewPrivilige(Boolean newPrivilige) {
		this.newPrivilige = newPrivilige;
	}


	public List<User> getUserList() {
		return userList;
	}



	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String updateRegisteredUser(){
		user =  userBean.findUserByName(this.name);
		if (user != null){
			user.setName(this.newName);
			user.setPassword(this.newPassword);
			user.setAdmin(this.newPrivilige);
			user = userBean.updateUser(user);
			return "success";
		}
		else{
			return "failure";
		}
	}

	public String deleteRegisteredUser(){
		try{
			user =  userBean.findUserByName(this.getName());
	
		}
		catch(IllegalArgumentException il){
			return "userDeleteFailure";
		}
		userBean.deleteUser(this.user);
		return "userDeleteSuccess";
	}
	
	public String undoRegisteredUser(){
		user =  userBean.findUserByName(this.name);
		if (user != null){
			user = userBean.undoUserChanges(user);
			return "success";
		}
		else{
			return "failure";
		}
	}

		

}
